<?php

namespace App\Http\Requests\Frontend\V10\Users;

use App\Rules\MobileCheck;
use App\Rules\PwdCheck;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Log;

class UserWechatsRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $params = $this->all();
        Log::debug('user_wechat_store', ['params'=>$params]);
        switch($this->method()) {
            /*
             * index
             */
            case 'GET': {
                if ($this->routeIs('wechat.index')) {
                    return [];
                } else if ($this->routeIs('wechat.show')) {
                    return [
                        'phone_num' => [
                            'required',
                            new MobileCheck(),
                        ],
                    ];
                } else {
                    return [];
                }
            }
            /*
             * store
             */
            case 'POST': {
                //微信登录 && 验证是否绑定
                return [
                    'openid' => 'required|string|max:28',
                    'unionid' => 'string|max:28',
                    'nickname' => 'string|max:255',
                    'gender' => 'integer|in:0,1,2',
                    'city' => 'string|max:50',
                    'province' => 'string|max:50',
                    'country' => 'string|max:50',
                    'head' => 'string|max:255',
                ];
            }
            /*
             * update
             */
            case 'PUT': {
                //注册 && 绑定
                return [
                    'phone_num' => [
                        'required',
                        new MobileCheck(),
                        'unique:users'
                    ],
                    'password' => 'required|alpha_num|min:6|max:18|confirmed',
                    'code'=>'required|digits_between:6,6|numeric',
                ];
            }
            case 'PATCH': {
                //输入手机号 密码 && 绑定
                return [
                    'phone_num' => [
                        'required',
                        new MobileCheck(),
                    ],
                    'password' => [
                        'required',
                        new PwdCheck(),
                    ],
                ];
            }
            default: {
                return [];
            }
        }
    }

    /**
     * 验证信息返回
     *
     * @return array
     */
    public function messages()
    {
        return [
            /*
             * 微信登录
             */
            'openid.required' => '缺少openid',
            'openid.max' => 'openid超出范围',
            'openid.string' => 'openid类型错误',
            'unionid.max' => 'unionid超出范围',
            'unionid.string' => 'unionid类型错误',
            'nickname.max' => '昵称超出范围',
            'nickname.string' => '昵称类型错误',
            'gender.integer' => '性别必须为整型',
            'gender.in' => '性别超出范围',
            'city.string' => '城市类型错误',
            'city.max' => '城市超出范围',
            'province.string' => '省份类型错误',
            'province.max' => '省份超出范围',
            'country.string' => '国家类型错误',
            'country.max' => '国家超出范围',
            'head.string' => '用户头像类型错误',
            'head.max' => '用户头像超出范围',
            /*
             * 注册账号
             */
            'phone_num.required' => '手机号码不能为空',
            'phone_num.unique' => '该手机号已存在',
            'password.required' => '密码不能为空',
            'password.alpha_num' => '密码必须为数字或字母',
            'password.min' => '密码长度最小为6位',
            'password.max' => '密码长度最长为18位',
            'password.confirmed' => '确认密码与密码不一致',
            'code.required'=>'验证码不能为空',
            'code.digits_between'=>'请输入6位验证码',
            'code.numeric'=>'验证码必须为数字',
        ];
    }
}
